﻿using BusinessLogicLayer;
using Microsoft.Reporting.WinForms;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace PresentationLayer
{
    public delegate void SetNVPV(string maNV);
    public partial class InHoSoForm : Form
    {
        public InHoSoForm()
        {
            InitializeComponent();
            this.reportViewerHoSo.ReportExport += reportViewerHoSo_ReportExport;
        }
        public SetPath SetPathFile { get; set; }
        private string MaNVPV;
        private void SetNVPV(string ma)
        {
            this.MaNVPV = ma;
        }
        void reportViewerHoSo_ReportExport(object sender, Microsoft.Reporting.WinForms.ReportExportEventArgs e)
        {
            CapNhatPVForm f = new CapNhatPVForm();
            f.SetNV = this.SetNVPV;
            DialogResult dlg = f.ShowDialog();
            if (dlg == DialogResult.OK)
            {
                if (MaNVPV != "")
                    new HoSoUngVienManager().CapNhatPhongVan(HoSo[0].MaHoSo, this.MaNVPV);
                e.Cancel = true;
                string extension = this.GetRenderingExtension(e.Extension);
                SaveFileDialog saveFileDialog = new SaveFileDialog()
                {
                    Title = "Save As",
                    CheckPathExists = true,
                    InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments),
                    Filter = e.Extension.LocalizedName + " (*" + extension + ")|*" + extension + "|All files(*.*)|*.*",
                    FilterIndex = 0
                };

                if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
                {
                    this.reportViewerHoSo.ExportDialog(e.Extension, e.DeviceInfo, saveFileDialog.FileName);

                    // Here's where I call my method to prompt user to open the file.
                    // System.Diagnostics.Process.Start(saveFileDialog.FileName);

                    SetPathFile(saveFileDialog.FileName);
                    DialogResult dlg1 = MessageBox.Show("Bạn có muốn gửi mail không file mới export không?", "Xác nhận", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
                    if (dlg1 == DialogResult.OK)
                    {
                        GuiMailForm f1 = new GuiMailForm();
                        f1.PathString = saveFileDialog.FileName;
                        this.Close();
                        f1.ShowDialog();
                    }
                }
            }
        }

        private string GetRenderingExtension(RenderingExtension extension)
        {
            switch (extension.Name)
            {
                case "PDF":
                    return ".pdf";
                case "EXCELOPENXML":
                    return ".xls";
                case "WORDOPENXML":
                    return ".doc";
            }
            return ".doc";
        }

        public List<HoSoUngVien> HoSo { get; set; }

        private void InHoSoForm_Load(object sender, EventArgs e)
        {

            HoSoUngVienBindingSource.DataSource = this.HoSo;
            this.reportViewerHoSo.RefreshReport();
        }
    }
}
